<template>
  <el-dialog :title="titlePark" append-to-body :close-on-click-modal="false" @close="closeDialog()" v-model="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="区域名称" prop="regionName">
            <el-input v-model="dataForm.regionName" placeholder="区域名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车位数" prop="spaceNum">
            <el-input v-model="dataForm.spaceNum" placeholder="车位数"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否月租" prop="isOnlyMonthly">
            <!-- <el-select v-model="dataForm.isOnlyMonthly" clearable class="m-2" placeholder="是否月租">
              <el-option v-for="item in isOrDisable" :key="item.value" :label="item.label" :value="item.value" />
            </el-select> -->
            <el-radio-group v-model="dataForm.isOnlyMonthly">
              <el-radio label="0" border>否</el-radio>
              <el-radio label="1" border>是</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from '@/api/stop/region'

export default {
  data () {
    return {
      visible: false,
      canSubmit: false,
      titlePark: null,
      dataForm: {
        id: '',
        parkId: '',
        isSync: '',
        regionName: '',
        createBy: '',
        updateBy: '',
        createTime: '',
        updateTime: '',
        spaceNum: '',
        isOnlyMonthly: '',
      },
      dataRule: {
        parkId: [
          { required: true, message: '停车场id不能为空', trigger: 'blur' }
        ],

        isSync: [
          { required: true, message: '是否同步不能为空', trigger: 'blur' }
        ],

        regionName: [
          { required: true, message: '区域名称不能为空', trigger: 'blur' }
        ],

        spaceNum: [
          { required: true, message: '车位数不能为空', trigger: 'blur' }
        ],

        isOnlyMonthly: [
          { required: true, message: '是否仅月租不能为空', trigger: 'blur' }
        ],

      },
      isOrDisable: [
        { value: 0, label: "否" },
        { value: 1, label: "是" },
      ]
    }
  },
  methods: {
    init (id) {
      if (id == undefined) {
        this.titlePark = '新增'
      } else {
        this.titlePark = '修改'
      }
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (id) {
          getObj(id).then(response => {
            this.dataForm = response.data.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            this.dataForm.parkId = this.$route.query.parkId
            addObj(this.dataForm).then(data => {
              this.$notify.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    },
    //重置表单
    closeDialog () {
      this.$refs["dataForm"].resetFields()
    }
  }
}
</script>
